package com.tjsinfo.tjsoa.bylaws.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.jbpm.taskmgmt.exe.TaskInstance;
import org.json.JSONArray;
import org.json.JSONObject;
import org.omg.CORBA.Request;

import com.tjsinfo.tjsoa.bylaws.bo.IBylawsBO;
import com.tjsinfo.tjsoa.bylaws.vo.Bylaws;
import com.tjsinfo.tjsoa.workflow.WorkFlowTemplate;

public class GuiZhangZhiDuShenPiAction  extends DispatchAction{
	
	private IBylawsBO bylawsBO;
	
	public IBylawsBO getBylawsBO() {
		return bylawsBO;
	}

	public void setBylawsBO(IBylawsBO bylawsBO) {
		this.bylawsBO = bylawsBO;
	}

	/**
	 * 控制
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward kongzhi(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		WorkFlowTemplate workFlowTemplate = new WorkFlowTemplate();
		Long id = Long.valueOf(arg2.getParameter("taskid"));
		Bylaws  bylaws =  bylawsBO.findById(arg2.getParameter("id"));
		bylaws.setTaskid(id);
		arg2.setAttribute("bylaws", bylaws);
		TaskInstance taskInstance   = workFlowTemplate.nextStep(id);
		if (taskInstance.getDescription().equals("审批")) {
			return arg0.findForward("shenhe");
		}
		return arg0.findForward("success");
	}
	/**
	 * 审批
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward shenpi(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		WorkFlowTemplate workFlowTemplate = new WorkFlowTemplate();
		Bylaws  bylaws =  bylawsBO.findById(arg2.getParameter("id"));
		bylaws.setTitle(arg2.getParameter("title"));
		bylaws.setContent(arg2.getParameter("contest"));
		bylaws.setCreateDate(arg2.getParameter("createDate"));
		
		if(!"".equals(arg2.getParameter("ids"))&&arg2.getParameter("ids") !=null  )
		{
			String ids = arg2.getParameter("ids");
			workFlowTemplate.createTaskInstance(Long.valueOf(arg2.getParameter("taskid")), ids);
			bylawsBO.updateBylaws(bylaws);
		}
		else
		{
			bylaws.setAuditing_sign("1");
			bylawsBO.updateBylaws(bylaws);
		}
		workFlowTemplate.nextStep(Long.valueOf(arg2.getParameter("taskid")) , null);
		// 转向，并传递提示语
		if("3".equals(bylaws.getCid())){
			arg2.setAttribute("linkurl", "bylaws/guizhangzhidudaiban.jsp");
		}else{
			arg2.setAttribute("linkurl", "bylaws/tongzhidaiban.jsp");
		}
		
		arg2.setAttribute("message", "bylaws.message.save");
		return arg0.findForward("success");
	}
}
